package com.iterator.service.web.promotionService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.zql.InfoData;
import com.iterator.bean.web.pojo.zql.Coupon;
import com.iterator.bean.web.pojo.zql.CouponExample;
import com.iterator.bean.web.pojo.zql.CouponUser;
import com.iterator.bean.web.pojo.zql.CouponUserExample;
import com.iterator.bean.web.zql.bo.CouponBO;
import com.iterator.bean.web.zql.bo.CouponUserBO;
import com.iterator.mapper.web.promotionMapper.CouponMapper;
import com.iterator.mapper.web.promotionMapper.CouponUserMapper;
import com.iterator.util.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @Author : L
 * @ProjectName : project-2
 * @Date : 1/9/2021
 * @Description :
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public InfoData<Coupon> queryCouponList(CouponBO couponBO) {
        PageHelper.startPage(couponBO.getPage(), couponBO.getLimit());

        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(couponBO.getSort() + " " + couponBO.getOrder());
        CouponExample.Criteria criteria = couponExample.createCriteria();
        if (couponBO.getName() != null) {
            criteria.andNameLike("%"+couponBO.getName()+"%");
        }
        if (couponBO.getType() != null) {
            criteria.andTypeEqualTo(couponBO.getType());
        }
        if (couponBO.getStatus() != null) {
            criteria.andStatusEqualTo(couponBO.getStatus());
        }
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        InfoData<Coupon> couponInfoData = new InfoData<>();
        couponInfoData.setItems(coupons);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();
        couponInfoData.setTotal((int) total);

        return couponInfoData;
    }

    @Override
    public Coupon insertCoupon(Coupon coupon) {
        if (coupon.getType() == 2) {
            String s = RandomCodeUtil.usingRandom();
            coupon.setCode(s);
        }
        Date addTime = new Date();
        coupon.setAddTime(addTime);
        couponMapper.insertSelective(coupon);
        return coupon;
    }

    @Override
    public void updateCoupon(Coupon coupon) {
        // 生成优惠券码
        if (coupon.getType() == 2) {
            String s = RandomCodeUtil.usingRandom();
            coupon.setCode(s);
        }
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public Coupon queryCoupon(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public void deleteCoupon(Integer id) {
        Coupon coupon = new Coupon();
        coupon.setId(id);
        coupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public InfoData<CouponUser> queryCouponUserList(CouponUserBO couponUserBO) {
        PageHelper.startPage(couponUserBO.getPage(), couponUserBO.getLimit());
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(couponUserBO.getSort() + " " + couponUserBO.getOrder());
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        if (couponUserBO.getCouponId() != null) {
            criteria.andCouponIdEqualTo(couponUserBO.getCouponId());
        }
        if (couponUserBO.getUserId() != null) {
            criteria.andUserIdEqualTo(couponUserBO.getUserId());
        }
        if (couponUserBO.getStatus() != null) {
            criteria.andStatusEqualTo(couponUserBO.getStatus());
        }
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        InfoData<CouponUser> couponUserInfoData = new InfoData<>();
        couponUserInfoData.setItems(couponUsers);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>();
        long total = couponUserPageInfo.getTotal();
        couponUserInfoData.setTotal((int) total);

        return couponUserInfoData;
    }


}
